﻿/*
	zealotwms 仓库管理系统
	Copyright (C) 2011  狂热者工作组 
    内容：MainForm 主窗体
    时间: 2011-9-5
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Zwms.Client.CommonUI;
using Zwms.Data.BusinessObjects;
using Zwms.Client.Helper;
using Zwms.Data.CommonObject;

namespace Zwms.Client
{
    public partial class MainForm : BaseForm
    {
        #region 成员变量
        public static SysAccount CurrentUser { get; set; }
        public WaitForm _waitWindow { get; set; }
        #endregion

        #region 构造函数
        public MainForm()
        {
            InitializeComponent();
            InitControl();
        }
        #endregion

        #region 私有函数/方法
        //初始化窗体控件内容
        //1.载入主菜单&树形菜单
        //3.载入起始页内容
        private void InitControl()
        {
            SplashScreen.SetCommentaryString("载入系统菜单...");
            LoadMenu();
            InitStartPage();
            InitStatusBar();
            LoadInPlanBillList();
            LoadInBillList();
            LoadOutPlanBillList();
            LoadOutBillList();
            SplashScreen.EndDisplay();
        }

        private void InitStatusBar()
        {
            lbStatus.Text = string.Format("当前人员:{0} 仓库:{1}", MainForm.CurrentUser.UserName, MainForm.CurrentUser.WarehouseName);
        }

        private void LoadMenu()
        {
             //初始化菜单栏
            menuMain.Items.Clear();
            tvMenu.Nodes.Clear();
           ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
            IList<SysMenu> menuList = client.GetMenuByRoleCode(MainForm.CurrentUser.UserRole);
            IList<SysMenu> rootMenuList = menuList.Where(c => c.ParMenuCode.Trim() == "0").ToList();
            TreeNode rootNode = new TreeNode(string.Format("仓库管理系统"));
            foreach (SysMenu rootMenu in rootMenuList)
            {
                IList<SysMenu> subMenuList = menuList.Where(c => c.ParMenuCode.Trim() == rootMenu.MenuCode.Trim()).ToList();
                ToolStripMenuItem rootMenuItem = new ToolStripMenuItem();
                rootMenuItem.Text = rootMenu.MenuName.Trim();
                TreeNode node = new TreeNode(rootMenu.MenuName.Trim());
                foreach (SysMenu subMenu in subMenuList)
                {
                    ToolStripMenuItem subMenuItem = new ToolStripMenuItem();
                    subMenuItem.Image = (System.Drawing.Bitmap)Zwms.Client.Properties.Resources.ResourceManager.GetObject(subMenu.MenuName.Trim());
                    subMenuItem.Text = subMenu.MenuName.Trim();
                    subMenuItem.Click += new EventHandler(menuItem_Click);

                    rootMenuItem.DropDownItems.Add(subMenuItem);

                    TreeNode n = new TreeNode(subMenu.MenuName.Trim());
                    n.Tag = "function";
                    node.Nodes.Add(n);
                }
                menuMain.Items.Add(rootMenuItem);
                rootNode.Nodes.Add(node);
            }
           
            rootNode.ExpandAll();
            tvMenu.Nodes.Add(rootNode);
        }

        private void InitStartPage()
        {
            //
        }

        private void MenuItemClick(string sTitle)
        {
            switch (sTitle)
            {
                case "重新登录":
                    if (MessageBox.Show("您确定要退出，更换其他账号登录吗？", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
                        Application.Restart();
                    }
                    break;
                //入库
                case "入库计划":
                    {
                        InWms.InPlanBillWnd window = new InWms.InPlanBillWnd(this);
                        AddTablePage(sTitle, window, false);
                    }
                    break;
                case "扫描收货":
                    {
                        InWms.InBillWnd window = new InWms.InBillWnd();
                        AddTablePage(sTitle, window, false);
                    }
                    break;
                case "库位分配":
                    {
                        InWms.InStockWnd window = new InWms.InStockWnd();
                        AddTablePage(sTitle, window, false);
                    }
                    break;

                //出库
                case "出库计划":
                    {
                        OutWms.OutPlanBillWnd window = new OutWms.OutPlanBillWnd(this);
                        AddTablePage(sTitle, window, false);
                    }
                    break;
                case "扫描发货":
                    {
                        OutWms.OutBillWnd window = new OutWms.OutBillWnd();
                        AddTablePage(sTitle, window, false);
                    }
                    break;
                case "发货单":
                    {
                    }
                    break;

                //库存管理
                case "库存查询":
                    {
                        ManageWms.StockQueryWnd window = new ManageWms.StockQueryWnd();
                        AddTablePage("库存查询", window, false);
                    }
                    break;
                case "库存变更查询":
                    {
                    }
                    break;
                case "货物移库":
                    {
                    }
                    break;
                case "货物移位":
                    {
                        ManageWms.TransferBillWnd window = new ManageWms.TransferBillWnd();
                        AddTablePage("库存移动", window, false);
                    }
                    break;
                case "库存盘点":
                    {
                        ManageWms.CountBillCreatWnd window = new ManageWms.CountBillCreatWnd();
                        AddTablePage("库存盘点", window, false);
                    }
                    break;
                case "盘点结果确认":
                    {
                        ManageWms.CountBillResultWnd window = new ManageWms.CountBillResultWnd();
                        AddTablePage("盘点结果确认", window, false);
                    }
                    break;
                case "库存调整":
                    {
                        ManageWms.StockAdjustWnd window = new ManageWms.StockAdjustWnd();
                        AddTablePage("库存调整", window, false);
                    }
                    break;
                case "货物统计":
                    {
                        ManageWms.StockGoodsStatWnd window = new ManageWms.StockGoodsStatWnd();
                        AddTablePage("货物统计", window, false);
                    }
                    break;
              
                //仓库数据维护    
                case "货物分类":
                    {
                        InfoWms.GoodsCategoryWnd window = new InfoWms.GoodsCategoryWnd();
                        AddTablePage(sTitle, window, false);
                    }
                    break;
                case "货物信息":
                    {
                        InfoWms.GoodsWnd window = new InfoWms.GoodsWnd();
                        AddTablePage(sTitle, window, false);
                    }
                    break;
                case "部门信息":
                    {
                        InfoWms.DepartmentWnd window = new InfoWms.DepartmentWnd();
                        AddTablePage(sTitle, window, false);
                    }
                    break;
                case "地区信息":
                    {
                        InfoWms.RegionWnd window = new InfoWms.RegionWnd();
                        AddTablePage(sTitle, window, false);
                    }
                    break;
                case "公司信息":
                    {
                        InfoWms.CompanyWnd window = new InfoWms.CompanyWnd();
                        AddTablePage(sTitle, window, false);
                    }
                    break;
                case "计量单位":
                    {
                        InfoWms.UnitWnd window = new InfoWms.UnitWnd();
                        AddTablePage(sTitle, window, false);
                    }
                    break;

                case "仓库信息":
                    {
                        InfoWms.WarehouseWnd window = new InfoWms.WarehouseWnd();
                        AddTablePage(sTitle, window, false);
                    }
                    break;

                case "库区信息":
                    {
                        InfoWms.AreaWnd window = new InfoWms.AreaWnd();
                        AddTablePage(sTitle, window, false);
                    }
                    break;

                case "货架信息":
                    {
                        InfoWms.ShelfWnd window = new InfoWms.ShelfWnd();
                        AddTablePage(sTitle, window, false);
                    }
                    break;

                case "库位信息":
                    {
                        InfoWms.LocationWnd window = new InfoWms.LocationWnd();
                        AddTablePage(sTitle, window, false);
                    }
                    break;

                //系统维护
                case "登录信息查询":
                    {
                        InfoSys.SysLoginInfoQueryWnd window = new InfoSys.SysLoginInfoQueryWnd();
                        AddTablePage(sTitle, window, false);
                    }
                    break;
                case "操作日志查询":
                    {
                        InfoSys.SysOptLogQueryWnd window = new InfoSys.SysOptLogQueryWnd();
                        AddTablePage(sTitle, window, false);
                    }
                    break;
                case "系统日志维护":
                    {
                    }
                    break;
                case "系统角色维护":
                    {
                        InfoSys.SysRoleWnd window = new InfoSys.SysRoleWnd();
                        AddTablePage(sTitle, window, false);
                    }
                    break;
                case "系统用户维护":
                    {
                        InfoSys.SysAccountWnd window = new InfoSys.SysAccountWnd();
                        AddTablePage(sTitle, window, false);
                    }
                    break;
                case "系统设置":
                    {
                        InfoSys.SysConfigWnd window = new InfoSys.SysConfigWnd();
                        AddTablePage(sTitle, window,false);
                    }
                    break;
                case "修改密码":
                    {
                        InfoSys.PasswordModifyWnd window = new InfoSys.PasswordModifyWnd();
                        AddTablePage(sTitle, window, false);
                    }
                    break;
                case "刷新":
                    {
                        this.InitControl();
                    }
                    break;
                default: 
                    break;
            }
        }

        public void AddTablePage(string title, UserControl ctrl,bool bAddNew)
        {
            for (int i = 0; i < tabMain.TabPages.Count; i++)
            {
                TabPage page = tabMain.TabPages[i];

                if (page.Text.Trim() == title)
                {
                    if (bAddNew)
                    {
                        tabMain.TabPages.RemoveAt(i);
                    }
                    else
                    {
                        tabMain.SelectedIndex = i;
                        return;
                    }
                }
            }
            TabPage tp = new TabPage(title);
            ctrl.Dock = DockStyle.Fill;

            tp.Controls.Add(ctrl);
            this.tabMain.TabPages.Add(tp);
            this.tabMain.SelectedIndex = this.tabMain.TabPages.Count - 1;
        }

        private void LoadInPlanBillList()
        {
            ServiceReferenceZwms.ServiceZwmsClient client
                = new ServiceReferenceZwms.ServiceZwmsClient();
            BilInPlanBill queryrecord = new BilInPlanBill();
            PagerIndex pIndex = new PagerIndex(1,int.MaxValue);
            IList<BilInPlanBill> recordList =
                client.QueryInPlanBillList(queryrecord, ref pIndex)
                .Where(c=>c.BillStatus =="0" ||c.BillStatus =="1").ToArray();
            foreach (BilInPlanBill record in recordList)
            {
                record.BillStatus = CommonDefine.DictIPBillStatus[record.BillStatus];
            }
            this.bilInPlanBillBindingSource.DataSource = null;
            this.gbInPlanBill.Text = string.Format("入库计划单({0})", recordList.Count);
            if (recordList.Count > 0)
            {
                this.bilInPlanBillBindingSource.DataSource = recordList;
            }
        }

        private void LoadInBillList()
        {
            ServiceReferenceZwms.ServiceZwmsClient client
                = new ServiceReferenceZwms.ServiceZwmsClient();
            BilInBill queryrecord = new BilInBill();
            PagerIndex pIndex = new PagerIndex(1, int.MaxValue);
            IList<BilInBill> recordList =
                client.QueryInBillList(queryrecord, ref pIndex)
                .Where(c => c.BillStatus == "0" || c.BillStatus == "1" ).ToArray();
            foreach (BilInBill record in recordList)
            {
                record.BillStatus = CommonDefine.DictInBillStatus[record.BillStatus];
            }
            this.bilInBillBindingSource.DataSource = null;
            this.gbInBill.Text = string.Format("入库单({0})", recordList.Count);
            if (recordList.Count > 0)
            {
                this.bilInBillBindingSource.DataSource = recordList;
            }
        }

        private void LoadOutPlanBillList()
        {
            ServiceReferenceZwms.ServiceZwmsClient client
                = new ServiceReferenceZwms.ServiceZwmsClient();
            BilOutPlanBill queryrecord = new BilOutPlanBill();
            PagerIndex pIndex = new PagerIndex(1, int.MaxValue);
            IList<BilOutPlanBill> recordList =
                client.QueryOutPlanBillList(queryrecord, ref pIndex)
                .Where(c => c.BillStatus == "0" || c.BillStatus == "1" || c.BillStatus == "2").ToArray();
            foreach (BilOutPlanBill record in recordList)
            {
                record.BillStatus = CommonDefine.DictOPBillStatus[record.BillStatus];
                record.OutType = CommonDefine.DictOBType[record.OutType];
            }
            this.bilOutPlanBillBindingSource.DataSource = null;
            this.gbOutPlanBill.Text = string.Format("出库计划单({0})", recordList.Count);
            if (recordList.Count > 0)
            {
                this.bilOutPlanBillBindingSource.DataSource = recordList;
            }
        }

        private void LoadOutBillList()
        {
            ServiceReferenceZwms.ServiceZwmsClient client
                = new ServiceReferenceZwms.ServiceZwmsClient();
            BilOutBill queryrecord = new BilOutBill();
            PagerIndex pIndex = new PagerIndex(1, int.MaxValue);
            IList<BilOutBill> recordList =
                client.QueryOutBillList(queryrecord, ref pIndex)
                .Where(c => c.BillStatus == "0" || c.BillStatus == "1").ToArray();
            foreach (BilOutBill record in recordList)
            {
                record.BillStatus = CommonDefine.DictOutBillStatus[record.BillStatus];
                record.OutType = CommonDefine.DictOBType[record.OutType];
            }
            this.bilOutBillBindingSource.DataSource = null;
            this.gbOutBill.Text = string.Format("出库单({0})", recordList.Count);
            if (recordList.Count > 0)
            {
                this.bilOutBillBindingSource.DataSource = recordList;
            }
        }

        #endregion

        #region 控件函数
        private void menuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
            MenuItemClick(menuItem.Text);
        }

        private void tabControl_DoubleClick(object sender, EventArgs e)
        {
            TabControl tc = (TabControl)sender;
            TabPage tp = tc.TabPages[tc.SelectedIndex];
            if (tp.Text != "起始页")
            {
                tc.TabPages.Remove(tp);
                this.tabMain.SelectedIndex = this.tabMain.TabPages.Count - 1;
            }
        }

        private void tvMain_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            TreeNode node = e.Node;
            if (node.Tag != null && node.Tag.ToString() == "function")
            {
                MenuItemClick(node.Text);
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("您确定要退出系统吗？", "提示", MessageBoxButtons.OKCancel)
                != System.Windows.Forms.DialogResult.OK)
            {
               e.Cancel = true;
            }
            else
            {
                ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                DateTime dtNow = client.GetCurrentSystemTime();
                MainForm.CurrentUser.LastLogoutDate = CommonHelper.GetDateString(dtNow);
                MainForm.CurrentUser.LastLogoutTime = CommonHelper.GetTimeString(dtNow);
                client.UpdateAccount(MainForm.CurrentUser);
                e.Cancel = false;
            }
        }

        private void dgvInPlanBill_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (dgvInPlanBill.CurrentCell == null)
            {
                MessageBox.Show("请首先在列表中选中项");
                return;
            }
            int rowIndex = dgvInPlanBill.CurrentCell.RowIndex;

            if (rowIndex < 0)
            {
                MessageBox.Show("请首先在列表中选中项！");
                return;
            }
            DataGridViewRow row = dgvInPlanBill.Rows[rowIndex];
            BilInPlanBill selectedRecord = row.DataBoundItem as BilInPlanBill;

            if (MainForm.CurrentUser.UserRole.Trim() == "01" || MainForm.CurrentUser.UserRole.Trim() == "02")
            {
                //无限制
            }
            else if (MainForm.CurrentUser.UserRole.Trim() == "03")
            {
                //判断是否为该用户的入库计划单
                if (selectedRecord.CreateUserCode != MainForm.CurrentUser.Id)
                {
                    MessageBox.Show("对不起，您不具备该入库计划单的操作权限！");
                    return;
                }
            }

            InWms.InPlanBillWnd window = new InWms.InPlanBillWnd(this,selectedRecord.Id);
            this.AddTablePage("入库计划", window, true);
        }

        private void dgvInBill_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (dgvInBill.CurrentCell == null)
            {
                MessageBox.Show("请首先在列表中选中项");
                return;
            }
            int rowIndex = dgvInBill.CurrentCell.RowIndex;

            if (rowIndex < 0)
            {
                MessageBox.Show("请首先在列表中选中项！");
                return;
            }
            DataGridViewRow row = dgvInBill.Rows[rowIndex];
            BilInBill selectedRecord = row.DataBoundItem as BilInBill;

            if (MainForm.CurrentUser.UserRole.Trim() == "01" || MainForm.CurrentUser.UserRole.Trim() == "02")
            {
                //无限制
            }
            else if (MainForm.CurrentUser.UserRole.Trim() == "03")
            {
                //判断是否为该用户的入库计划单
                if (selectedRecord.CreateUserCode == "")
                {
                    //
                }
                else if (selectedRecord.CreateUserCode != MainForm.CurrentUser.Id)
                {
                    MessageBox.Show("对不起，您不具备该入库单的操作权限！");
                    return;
                }
            }

            foreach (KeyValuePair<string, string> kvp in CommonDefine.DictInBillStatus)
            {
                if (selectedRecord.BillStatus == kvp.Value)
                    selectedRecord.BillStatus = kvp.Key;
            }

            if (selectedRecord.BillStatus == "0")
            {
                if (MessageBox.Show(string.Format("开始对入库单 {0} 进行收货？", selectedRecord.Id), "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                {
                    selectedRecord.BillStatus = CommonDefine.DictInBillStatus[selectedRecord.BillStatus];
                    return;
                }
                InWms.ReceiveForm window = new InWms.ReceiveForm(selectedRecord.IBPlanCode, selectedRecord.Id);
                window.ShowDialog();
            }
            else if (selectedRecord.BillStatus == "1")
            {
                if (MessageBox.Show(string.Format("开始对入库单 {0} 进行库位分配？", selectedRecord.Id), "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                {
                    selectedRecord.BillStatus = CommonDefine.DictInBillStatus[selectedRecord.BillStatus];
                    return;
                }
                InWms.AssignLocationForm window = new InWms.AssignLocationForm(selectedRecord);
                window.ShowDialog();
            }
        }

        private void dgvOutPlanBill_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (dgvOutPlanBill.CurrentCell == null)
            {
                MessageBox.Show("请首先在列表中选中项");
                return;
            }
            int rowIndex = dgvOutPlanBill.CurrentCell.RowIndex;

            if (rowIndex < 0)
            {
                MessageBox.Show("请首先在列表中选中项！");
                return;
            }
            DataGridViewRow row = dgvOutPlanBill.Rows[rowIndex];
            BilOutPlanBill selectedRecord = row.DataBoundItem as BilOutPlanBill;


            if (MainForm.CurrentUser.UserRole.Trim() == "01" || MainForm.CurrentUser.UserRole.Trim() == "02")
            {
                //无限制
            }
            else if (MainForm.CurrentUser.UserRole.Trim() == "03")
            {
                //判断是否为该用户的入库计划单
                if (selectedRecord.CreateUserCode != MainForm.CurrentUser.Id)
                {
                    MessageBox.Show("对不起，您不具备该出库计划单的操作权限！");
                    return;
                }
            }

            OutWms.OutPlanBillWnd window = new OutWms.OutPlanBillWnd(this, selectedRecord.Id);
            this.AddTablePage("出库计划", window, true);
        }

        private void dgvOutBill_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (dgvOutBill.CurrentCell == null)
            {
                MessageBox.Show("请首先在列表中选中项");
                return;
            }
            int rowIndex = dgvOutBill.CurrentCell.RowIndex;

            if (rowIndex < 0)
            {
                MessageBox.Show("请首先在列表中选中项！");
                return;
            }
            DataGridViewRow row = dgvOutBill.Rows[rowIndex];
            BilOutBill selectedRecord = row.DataBoundItem as BilOutBill;

            if (MainForm.CurrentUser.UserRole.Trim() == "01" || MainForm.CurrentUser.UserRole.Trim() == "02")
            {
                //无限制
            }
            else if (MainForm.CurrentUser.UserRole.Trim() == "03")
            {
                //判断是否为该用户的入库计划单
                if (selectedRecord.CreateUserCode != MainForm.CurrentUser.Id)
                {
                    MessageBox.Show("对不起，您不具备该出库单的操作权限！");
                    return;
                }
            }

            foreach (KeyValuePair<string, string> kvp in CommonDefine.DictOutBillStatus)
            {
                if (selectedRecord.BillStatus == kvp.Value)
                    selectedRecord.BillStatus = kvp.Key;
            }

            foreach (KeyValuePair<string, string> kvp in CommonDefine.DictOBType)
            {
                if (selectedRecord.OutType == kvp.Value)
                    selectedRecord.OutType = kvp.Key;
            }


            OutWms.OutBillWnd window = new OutWms.OutBillWnd(selectedRecord);
            this.AddTablePage("出库单", window, true);
        }
        #endregion
    }
}
